{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "674bb13b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "31091364",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>一</th>\n",
       "      <th>二</th>\n",
       "      <th>三</th>\n",
       "      <th>四</th>\n",
       "      <th>五</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   一  二  三  四  五\n",
       "a  1  2  6  6  1\n",
       "b  5  5  4  3  1\n",
       "c  5  6  5  6  0\n",
       "d  4  4  0  6  1\n",
       "e  5  6  0  3  4"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1=pd.DataFrame(np.random.randint(0,7,(5,5)),\n",
    "            index=list('abcde'),\n",
    "            columns=list('一二三四五'))\n",
    "df1\n",
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a7926083",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "一    1\n",
       "二    2\n",
       "三    6\n",
       "四    6\n",
       "五    1\n",
       "Name: a, dtype: int32"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.iloc[0,:]  # numpy 数据提取 行,列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0d42a286",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    2\n",
       "b    5\n",
       "c    6\n",
       "d    4\n",
       "e    6\n",
       "Name: 二, dtype: int32"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.iloc[:,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "cbe00178",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>一</th>\n",
       "      <th>二</th>\n",
       "      <th>三</th>\n",
       "      <th>四</th>\n",
       "      <th>五</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   一  二  三  四  五\n",
       "a  1  2  6  6  1\n",
       "c  5  6  5  6  0"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.iloc[0:4:2,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "54ee856d",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>一</th>\n",
       "      <th>二</th>\n",
       "      <th>三</th>\n",
       "      <th>四</th>\n",
       "      <th>五</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   一  二  三  四  五\n",
       "a  1  2  6  6  1\n",
       "b  5  5  4  3  1\n",
       "c  5  6  5  6  0"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.iloc[0:3,:] # iloc用：左闭右开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4e0d005a",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>三</th>\n",
       "      <th>四</th>\n",
       "      <th>五</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   三  四  五\n",
       "a  6  6  1\n",
       "b  4  3  1\n",
       "c  5  6  0"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.iloc[[0,1,2],[2,3,4]] #"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "27142081",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['价', '海', '检']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lists=list('的房价过快海螺牌检查v看不了你')\n",
    "lists[2:10:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "13ffaf4b",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.91809061, 0.82217656, 0.52507665],\n",
       "       [0.4733232 , 0.43465703, 0.78361186],\n",
       "       [0.98136907, 0.86587484, 0.51450634]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1=np.random.random((6,8))\n",
    "arr1[[0,1,4],0:8:3]  # 单个的索引（内部/自定义），列表，start：stop：step"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae7676fe",
   "metadata": {},
   "source": [
    "# 文件读写"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c2f6275",
   "metadata": {},
   "source": [
    "## 写文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "342e213e",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>一</th>\n",
       "      <th>二</th>\n",
       "      <th>三</th>\n",
       "      <th>四</th>\n",
       "      <th>五</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   一  二  三  四  五\n",
       "a  1  2  6  6  1\n",
       "b  5  5  4  3  1\n",
       "c  5  6  5  6  0\n",
       "d  4  4  0  6  1\n",
       "e  5  6  0  3  4"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "2a3e4c87",
   "metadata": {},
   "outputs": [],
   "source": [
    "# excel\n",
    "df1.to_excel('Excel_df1.xlsx',\n",
    "            index=False,\n",
    "            header=None)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "37986d5b",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1.to_csv('Csv_df1.csv',\n",
    "            index=False,\n",
    "            header=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "0a01bddc",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1.to_csv('TexT_df1.txt',  \n",
    "#             index=False,\n",
    "          )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ef3f931",
   "metadata": {},
   "source": [
    "## 读数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "80b1d852",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>订单号</th>\n",
       "      <td>销售日期</td>\n",
       "      <td>销售人员</td>\n",
       "      <td>地区</td>\n",
       "      <td>城市</td>\n",
       "      <td>家电品牌</td>\n",
       "      <td>单价</td>\n",
       "      <td>数量（台）</td>\n",
       "      <td>销售额</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10240</th>\n",
       "      <td>2009-01-02 00:00:00</td>\n",
       "      <td>张三</td>\n",
       "      <td>华北</td>\n",
       "      <td>北京</td>\n",
       "      <td>奥克斯</td>\n",
       "      <td>1200</td>\n",
       "      <td>4</td>\n",
       "      <td>4800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10241</th>\n",
       "      <td>2009-01-03 00:00:00</td>\n",
       "      <td>李四</td>\n",
       "      <td>华北</td>\n",
       "      <td>北京</td>\n",
       "      <td>格力</td>\n",
       "      <td>1300</td>\n",
       "      <td>5</td>\n",
       "      <td>6500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10242</th>\n",
       "      <td>2009-01-13 00:00:00</td>\n",
       "      <td>钱五</td>\n",
       "      <td>华北</td>\n",
       "      <td>北京</td>\n",
       "      <td>美的</td>\n",
       "      <td>1250</td>\n",
       "      <td>6</td>\n",
       "      <td>7500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10243</th>\n",
       "      <td>2009-01-14 00:00:00</td>\n",
       "      <td>赵六</td>\n",
       "      <td>华北</td>\n",
       "      <td>北京</td>\n",
       "      <td>春兰</td>\n",
       "      <td>1500</td>\n",
       "      <td>3</td>\n",
       "      <td>4500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10334</th>\n",
       "      <td>2009-11-25 00:00:00</td>\n",
       "      <td>王娜</td>\n",
       "      <td>西南</td>\n",
       "      <td>成都</td>\n",
       "      <td>奥克斯</td>\n",
       "      <td>1501</td>\n",
       "      <td>7</td>\n",
       "      <td>10507</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10335</th>\n",
       "      <td>2009-12-01 00:00:00</td>\n",
       "      <td>刘宇</td>\n",
       "      <td>西南</td>\n",
       "      <td>成都</td>\n",
       "      <td>格力</td>\n",
       "      <td>1400</td>\n",
       "      <td>2</td>\n",
       "      <td>2800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10336</th>\n",
       "      <td>2009-12-12 00:00:00</td>\n",
       "      <td>陈笑</td>\n",
       "      <td>西南</td>\n",
       "      <td>成都</td>\n",
       "      <td>志高</td>\n",
       "      <td>1400</td>\n",
       "      <td>9</td>\n",
       "      <td>12600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10337</th>\n",
       "      <td>2009-12-23 00:00:00</td>\n",
       "      <td>汪俊</td>\n",
       "      <td>西南</td>\n",
       "      <td>昆明</td>\n",
       "      <td>春兰</td>\n",
       "      <td>1200</td>\n",
       "      <td>3</td>\n",
       "      <td>3600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10338</th>\n",
       "      <td>2009-12-26 00:00:00</td>\n",
       "      <td>齐易</td>\n",
       "      <td>西南</td>\n",
       "      <td>昆明</td>\n",
       "      <td>奥克斯</td>\n",
       "      <td>1200</td>\n",
       "      <td>5</td>\n",
       "      <td>6000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                         1     2   3   4     5     6      7      8\n",
       "0                                                                 \n",
       "订单号                   销售日期  销售人员  地区  城市  家电品牌    单价  数量（台）    销售额\n",
       "10240  2009-01-02 00:00:00    张三  华北  北京   奥克斯  1200      4   4800\n",
       "10241  2009-01-03 00:00:00    李四  华北  北京    格力  1300      5   6500\n",
       "10242  2009-01-13 00:00:00    钱五  华北  北京    美的  1250      6   7500\n",
       "10243  2009-01-14 00:00:00    赵六  华北  北京    春兰  1500      3   4500\n",
       "...                    ...   ...  ..  ..   ...   ...    ...    ...\n",
       "10334  2009-11-25 00:00:00    王娜  西南  成都   奥克斯  1501      7  10507\n",
       "10335  2009-12-01 00:00:00    刘宇  西南  成都    格力  1400      2   2800\n",
       "10336  2009-12-12 00:00:00    陈笑  西南  成都    志高  1400      9  12600\n",
       "10337  2009-12-23 00:00:00    汪俊  西南  昆明    春兰  1200      3   3600\n",
       "10338  2009-12-26 00:00:00    齐易  西南  昆明   奥克斯  1200      5   6000\n",
       "\n",
       "[100 rows x 8 columns]"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_excel('Excel数据.xlsx',\n",
    "             index_col=0, #行索引设置\n",
    "            header=None, # 列索引设置\n",
    "#              encoding  # 编码：UTF-8,GBK，\n",
    "              sheet_name=0 # 读第几张表\n",
    "             )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "820f856e",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>订单号</td>\n",
       "      <td>销售日期</td>\n",
       "      <td>销售人员</td>\n",
       "      <td>地区</td>\n",
       "      <td>城市</td>\n",
       "      <td>家电品牌</td>\n",
       "      <td>单价</td>\n",
       "      <td>数量（台）</td>\n",
       "      <td>销售额</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10240</td>\n",
       "      <td>2009-01-02 00:00:00</td>\n",
       "      <td>张三</td>\n",
       "      <td>华北</td>\n",
       "      <td>北京</td>\n",
       "      <td>奥克斯</td>\n",
       "      <td>1200</td>\n",
       "      <td>4</td>\n",
       "      <td>4800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10241</td>\n",
       "      <td>2009-01-03 00:00:00</td>\n",
       "      <td>李四</td>\n",
       "      <td>华北</td>\n",
       "      <td>北京</td>\n",
       "      <td>格力</td>\n",
       "      <td>1300</td>\n",
       "      <td>5</td>\n",
       "      <td>6500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10242</td>\n",
       "      <td>2009-01-13 00:00:00</td>\n",
       "      <td>钱五</td>\n",
       "      <td>华北</td>\n",
       "      <td>北京</td>\n",
       "      <td>美的</td>\n",
       "      <td>1250</td>\n",
       "      <td>6</td>\n",
       "      <td>7500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>10243</td>\n",
       "      <td>2009-01-14 00:00:00</td>\n",
       "      <td>赵六</td>\n",
       "      <td>华北</td>\n",
       "      <td>北京</td>\n",
       "      <td>春兰</td>\n",
       "      <td>1500</td>\n",
       "      <td>3</td>\n",
       "      <td>4500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>10334</td>\n",
       "      <td>2009-11-25 00:00:00</td>\n",
       "      <td>王娜</td>\n",
       "      <td>西南</td>\n",
       "      <td>成都</td>\n",
       "      <td>奥克斯</td>\n",
       "      <td>1501</td>\n",
       "      <td>7</td>\n",
       "      <td>10507</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>10335</td>\n",
       "      <td>2009-12-01 00:00:00</td>\n",
       "      <td>刘宇</td>\n",
       "      <td>西南</td>\n",
       "      <td>成都</td>\n",
       "      <td>格力</td>\n",
       "      <td>1400</td>\n",
       "      <td>2</td>\n",
       "      <td>2800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>10336</td>\n",
       "      <td>2009-12-12 00:00:00</td>\n",
       "      <td>陈笑</td>\n",
       "      <td>西南</td>\n",
       "      <td>成都</td>\n",
       "      <td>志高</td>\n",
       "      <td>1400</td>\n",
       "      <td>9</td>\n",
       "      <td>12600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>10337</td>\n",
       "      <td>2009-12-23 00:00:00</td>\n",
       "      <td>汪俊</td>\n",
       "      <td>西南</td>\n",
       "      <td>昆明</td>\n",
       "      <td>春兰</td>\n",
       "      <td>1200</td>\n",
       "      <td>3</td>\n",
       "      <td>3600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>10338</td>\n",
       "      <td>2009-12-26 00:00:00</td>\n",
       "      <td>齐易</td>\n",
       "      <td>西南</td>\n",
       "      <td>昆明</td>\n",
       "      <td>奥克斯</td>\n",
       "      <td>1200</td>\n",
       "      <td>5</td>\n",
       "      <td>6000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        0                    1     2   3   4     5     6      7      8\n",
       "0     订单号                 销售日期  销售人员  地区  城市  家电品牌    单价  数量（台）    销售额\n",
       "1   10240  2009-01-02 00:00:00    张三  华北  北京   奥克斯  1200      4   4800\n",
       "2   10241  2009-01-03 00:00:00    李四  华北  北京    格力  1300      5   6500\n",
       "3   10242  2009-01-13 00:00:00    钱五  华北  北京    美的  1250      6   7500\n",
       "4   10243  2009-01-14 00:00:00    赵六  华北  北京    春兰  1500      3   4500\n",
       "..    ...                  ...   ...  ..  ..   ...   ...    ...    ...\n",
       "95  10334  2009-11-25 00:00:00    王娜  西南  成都   奥克斯  1501      7  10507\n",
       "96  10335  2009-12-01 00:00:00    刘宇  西南  成都    格力  1400      2   2800\n",
       "97  10336  2009-12-12 00:00:00    陈笑  西南  成都    志高  1400      9  12600\n",
       "98  10337  2009-12-23 00:00:00    汪俊  西南  昆明    春兰  1200      3   3600\n",
       "99  10338  2009-12-26 00:00:00    齐易  西南  昆明   奥克斯  1200      5   6000\n",
       "\n",
       "[100 rows x 9 columns]"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_excel(r\"data\\Excel数据.xlsx\",\n",
    "#              index_col=0, #行索引设置\n",
    "#             header=None, # 列索引设置\n",
    "#              encoding  # 编码：UTF-8,GBK，\n",
    "              sheet_name=0 # 读第几张表\n",
    "             )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b351b844",
   "metadata": {},
   "outputs": [],
   "source": [
    "\"E:\\公司文件\\0、报销\\2022年报销 王右雪.xls\"\n",
    "\"C:\\Users\\wang\\Desktop\\民大\\Excel数据.xlsx\"\n",
    "\"data\\Excel数据.xlsx\"\n",
    "\"C:\\Users\\wang\\Desktop\\民大\\Pandas 学习二.ipynb\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "c75fa5b2",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   a  b  c\n",
       "0  1  1  1\n",
       "1  1  1  2\n",
       "2  1  2  1\n",
       "3  2  2  2\n",
       "4  2  3  1\n",
       "5  2  3  2"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv(r\"CSV数据.csv\",\n",
    "             index_col=0, #设置第几列做为行索引\n",
    "            header=0, # 是否将原来的第一行作为列索引\n",
    "#              encoding  # 编码：UTF-8,GBK，\n",
    "             )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "8901c4c3",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   0  1  2  3  4\n",
       "0  1  2  6  6  1\n",
       "1  5  5  4  3  1\n",
       "2  5  6  5  6  0\n",
       "3  4  4  0  6  1\n",
       "4  5  6  0  3  4"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv(r\"Csv_df1.csv\",\n",
    "#              index_col=0, #设置第几列做为行索引\n",
    "            header=None, # 是否将原来的第一行作为列索引\n",
    "#              encoding  # 编码：UTF-8,GBK，\n",
    "             )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "b9014df6",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   a  b  c\n",
       "0  1  1  1\n",
       "1  1  1  2\n",
       "2  1  2  1\n",
       "3  2  2  2\n",
       "4  2  3  1\n",
       "5  2  3  2"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv(r\"txt数据.txt\",\n",
    "             index_col=0, #设置第几列做为行索引\n",
    "#             header=None, # 是否将原来的第一行作为列索引\n",
    "#              encoding  # 编码：UTF-8,GBK，\n",
    "             )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6537dde",
   "metadata": {},
   "source": [
    "## 计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "ef341453",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.56"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(df1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "1bf088a1",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    3.2\n",
       "b    3.6\n",
       "c    4.4\n",
       "d    3.0\n",
       "e    3.6\n",
       "dtype: float64"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(df1,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "5f5473a0",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    3.2\n",
       "b    3.6\n",
       "c    4.4\n",
       "d    3.0\n",
       "e    3.6\n",
       "dtype: float64"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.mean(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "be4f2c4d",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    5\n",
       "b    5\n",
       "c    5\n",
       "d    5\n",
       "e    5\n",
       "dtype: int64"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.count(axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afb8baa9",
   "metadata": {},
   "source": [
    "# 数据处理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c368dfc9",
   "metadata": {},
   "source": [
    "## 缺失值处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "cffc4d6a",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     a    b   c\n",
       "2  1.0  4.0 NaN\n",
       "0  NaN  NaN NaN\n",
       "1  2.0  2.0 NaN\n",
       "3  3.0  1.0 NaN"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({\n",
    "    'a':[1,np.nan,2,3],\n",
    "    'b':[4,np.nan,2,1],\n",
    "    'c':[np.nan,np.nan,np.nan,np.nan]},\n",
    "    index=[2,0,1,3])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "1c3cc475",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    1\n",
       "b    1\n",
       "c    4\n",
       "dtype: int64"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.isnull().sum()  # 统计多少个缺失值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "39319f74",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     a    b   c\n",
       "2  1.0  4.0 NaN\n",
       "1  2.0  2.0 NaN\n",
       "3  3.0  1.0 NaN"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除缺失值\n",
    "df.dropna(how='all',axis=0,inplace=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "a69d4be8",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     a    b   c\n",
       "2  1.0  4.0 NaN\n",
       "0  1.0  4.0 NaN\n",
       "1  2.0  2.0 NaN\n",
       "3  3.0  1.0 NaN"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 填充缺失值\n",
    "df.fillna(3)\n",
    "df.fillna(method='ffill')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02a6705f",
   "metadata": {},
   "source": [
    "## 重复数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "53af8ffe",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>three</th>\n",
       "      <th>four</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   one  two  three  four\n",
       "b    0    0      0     0\n",
       "c    0    0      0     1\n",
       "c    2    6      6     2\n",
       "e    3    6      6     3"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df=pd.DataFrame({'one':[0,0,2,3],\n",
    "    'two':[0,0,6,6],\n",
    "    'three':[0,0,6,6],\n",
    "    'four':[0,1,2,3],},\n",
    "    index=['b','c','c','e'])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "a91779b8",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b    False\n",
       "c     True\n",
       "c    False\n",
       "e    False\n",
       "Name: one, dtype: bool"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['one'].duplicated()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "7f283b17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "      <th>three</th>\n",
       "      <th>four</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   one  two  three  four\n",
       "c    0    0      0     1\n",
       "e    3    6      6     3"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.drop_duplicates(subset=['one'],  #以哪些列作为重复的依据，不指定，检查整行\n",
    "                  )  # Tab\n",
    "df.drop_duplicates(subset=['two','three'],  #以哪些列作为重复的依据，不指定，检查整行\n",
    "                  keep='last',# 保留数据的位置\n",
    "                   inplace=False\n",
    "                  )  # Tab"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe205943",
   "metadata": {},
   "source": [
    "## 数据分箱"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "e6034c8e",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([77, 27, 62, 83, 79, 90, 25, 67, 69, 57, 85, 15, 89, 58, 88, 60, 50,\n",
       "       55, 10, 48])"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data=np.random.randint(0,100,20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "6a596804",
   "metadata": {},
   "outputs": [],
   "source": [
    "bins=[0,18,40,60,100] # 给定节点，相邻节点构成一个区间\n",
    "ans=pd.cut(data,bins,labels=['未成年','青年','中青年','老年'])\n",
    "# 连续数据离散化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "9dc54100",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 1, 3, 3, 3, 3, 1, 3, 3, 2, 3, 0, 3, 2, 3, 2, 2, 2, 0, 2],\n",
       "      dtype=int8)"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ans.codes # 返回所属的第几个区间的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "cf3e7085",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(74.0, 82.0], (26.0, 34.0], (58.0, 66.0], (82.0, 90.0], (74.0, 82.0], ..., (58.0, 66.0], (42.0, 50.0], (50.0, 58.0], (9.92, 18.0], (42.0, 50.0]]\n",
       "Length: 20\n",
       "Categories (10, interval[float64, right]): [(9.92, 18.0] < (18.0, 26.0] < (26.0, 34.0] < (34.0, 42.0] ... (58.0, 66.0] < (66.0, 74.0] < (74.0, 82.0] < (82.0, 90.0]]"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.cut(data,10) #等间距分开"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dcc06194",
   "metadata": {},
   "source": [
    "## 哑变量处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "be52c10a",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>[]~(￣▽￣)~*_司机</th>\n",
       "      <th>[]~(￣▽￣)~*_学生</th>\n",
       "      <th>[]~(￣▽￣)~*_导游</th>\n",
       "      <th>[]~(￣▽￣)~*_工人</th>\n",
       "      <th>[]~(￣▽￣)~*_教师</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   []~(￣▽￣)~*_司机  []~(￣▽￣)~*_学生  []~(￣▽￣)~*_导游  []~(￣▽￣)~*_工人  []~(￣▽￣)~*_教师\n",
       "0              0              0              0              1              0\n",
       "1              0              1              0              0              0\n",
       "2              1              0              0              0              0\n",
       "3              0              0              0              0              1\n",
       "4              0              0              1              0              0"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1=pd.DataFrame({'职业':['工人','学生','司机','教师','导游']})\n",
    "#使用 get_dummies()函数来进行哑变量转换\n",
    "pd.get_dummies(df1,prefix='[]~(￣▽￣)~*').astype(int)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5cc7fbc1",
   "metadata": {},
   "source": [
    "# 数据合并"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c7c1a97",
   "metadata": {},
   "source": [
    "## 按键（行列索引）合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "6593bb86",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1=pd.DataFrame({'first':[2,3,8],\n",
    "                'second':[4,5,6]},\n",
    "               index=['aa','b','c'])\n",
    "\n",
    "df2=pd.DataFrame({'second':[6,5,8],\n",
    "                'third':[17,18,19]},\n",
    "               index=['b','c','aa'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "51b92968",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>first</th>\n",
       "      <th>second</th>\n",
       "      <th>third</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>aa</th>\n",
       "      <td>2.0</td>\n",
       "      <td>4</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>3.0</td>\n",
       "      <td>5</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8.0</td>\n",
       "      <td>6</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>NaN</td>\n",
       "      <td>6</td>\n",
       "      <td>17.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>18.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>aa</th>\n",
       "      <td>NaN</td>\n",
       "      <td>8</td>\n",
       "      <td>19.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    first  second  third\n",
       "aa    2.0       4    NaN\n",
       "b     3.0       5    NaN\n",
       "c     8.0       6    NaN\n",
       "b     NaN       6   17.0\n",
       "c     NaN       5   18.0\n",
       "aa    NaN       8   19.0"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat((df1,df2),join='outer') # 两个列求并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "ecea652a",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>second</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>aa</th>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>aa</th>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    second\n",
       "aa       4\n",
       "b        5\n",
       "c        6\n",
       "b        6\n",
       "c        5\n",
       "aa       8"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat((df1,df2),join='inner') # 两个列求交集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6a67467",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
